const { app, BrowserWindow } = require("electron");

function createWindow() {
  const win = new BrowserWindow({
    width: 1000,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
    },
    frame: false,
  });

  // 由于macOS使用了另外的open-file事件获取文件参数，以下代码在macOS上很可能获取不到文件路径。
  if (
    process.argv.length >= 2 &&
    process.argv[1] !== "." &&
    process.argv[1] !== ""
  ) {
    win.loadURL("file://" + __dirname + "/App/editor.html?filepath=" + process.argv[1]);
  } else {
    win.loadFile("App/start.html");
  }

  win.on("close", (e) => {
    e.preventDefault();
  });
}

app.whenReady().then(createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});
